package com.neulogistics.contrib.warehouse.api.application.integrationevent.handler;

import com.blackto.lib.eventbus.abstraction.IIntegrationEventHandler;
import com.neulogistics.contrib.warehouse.api.application.integrationevent.subscribe.TaskOrderCreatedIntegrationEvent;
import com.neulogistics.contrib.warehouse.api.application.query.ICirculationOrderQueryService;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.po.CirculationOrderPO;
import com.neulogistics.contrib.warehouse.infrastructure.persistence.orm.service.ICirculationOrderPOService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Component
public class TaskOrderCreatedIntegrationEventHandler implements IIntegrationEventHandler<TaskOrderCreatedIntegrationEvent> {

    @Resource
    ICirculationOrderQueryService circulationOrderQueryService;

    @Resource
    ICirculationOrderPOService circulationOrderPOService;

    @Override
    @Transactional
    public CompletableFuture<Void> handle(TaskOrderCreatedIntegrationEvent event) {
        // 根据调度单id查询对应出入库单，置入taskId
        List<CirculationOrderPO> circulationOrderPOList =
                circulationOrderQueryService.getCirculationOrderByTransferId(event.transferOrderId);

        circulationOrderPOList.forEach(p -> p.setTaskId(event.taskId));

        circulationOrderPOService.updateBatchById(circulationOrderPOList);

        return CompletableFuture.completedFuture(null);
    }
}
